
var modelImgUrl='../../../images/';
const requestMode = require('../../../js/requestsMode.js');
const session = require('../../../js/session.js');
const  Tools = require('../../../js/Tools.js');
const constants = require('../../../js/constants.js');
Page({
    data:{
        choseImg:modelImgUrl+'rightArrow_black.png',
        way_array:[ {id:0,way:'换货'}, {id:1,way:'退货'} ],
        wayId:'',
        way:"选择退货/换货",
        xingxing:modelImgUrl+'xingxing.png',
        uploadImgLimit:modelImgUrl+'uploadImgLimit3.png',
        choseWayArray: [
            /* {id:0,name:'选择退/换货'},*/
            { id: 1, name: '退货' },
            { id: 2, name: '换货' }
        ],
        reasonArray:[],
        choseWayId: null,
        choseReasonId:null,
        upladImgData:[],
        back_buyer_refusal:'',
        allNotFailImg : []
    },
    wayChange_hx: function (e) {
        this.setData({   //给变量赋值
            choseWayId: e.detail.value
        });
    },
    uploads:function(){
        let weakSelf = this;
        let hasLength = weakSelf.data.upladImgData.length;
        let acceptUploadNumber = 3 - hasLength;
        let newImgArr = weakSelf.data.upladImgData;
        if(hasLength < 3){
            wx.chooseImage({
                count:3,
                success: function(res) {
                    var imageArray = res.tempFilePaths;
                    newImgArr = newImgArr.concat(imageArray);
                    weakSelf.circulateUploadImg(imageArray);
                    weakSelf.setData({
                        upladImgData:newImgArr
                    })
                }
            });
        }else{
            wx.showModal({
                title: '提示',
                content: '最多可上传3张图片'
            })
        }
    },
    previewImage: function(e){
        let This = this,
            index = e.currentTarget.dataset.index,
            pictures = This.data.upladImgData;
        wx.previewImage({
            current: pictures[index],
            urls: pictures,
            success:function(seeMaxImgSuccess){
                // console.log('查看success',seeMaxImgSuccess)
            },
            fail: function () {
                // console.log('查看fail')
            }
        });
    },
    reasonChange:function(e){
        this.setData({   //给变量赋值
            choseReasonId: e.detail.value
        });
    },
    onLoad: function (e) {
        let that = this;
        let orderId = e.orderId;
        requestMode.sendRequest('order/back-type','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (getTurnGoodsReasonSuccess) {
            let code = getTurnGoodsReasonSuccess.data.code;
            if(code == 200){
                that.setData({
                    reasonArray : getTurnGoodsReasonSuccess.data.data
                })
            }else{
                Tools.showErrorToast(getTurnGoodsReasonSuccess.data.message)
            }
        }, function (getTurnGoodsReasonFail) {
            Tools.showWrongNetWorkToast();
        });
        that.setData({
            orderId:orderId
        });
        requestMode.sendRequest('order/flush-back-images','POST',{order_id : orderId},{sKey:session.get('sKey')})
    },
    textareaChange:function(e){
        this.setData({
            back_buyer_refusal:e.detail.value
        })
    },
    submit:function(){
        let that = this;
        let orderId = that.data.orderId;
        let detailedReason = that.data.back_buyer_refusal;
        let refuseIndex = that.data.choseReasonId;
        if(refuseIndex != null){
            let selectSeasonId = that.data.reasonArray[that.data.choseReasonId].id;
            if( detailedReason ){
                let formData = {
                    order_id : orderId,
                    back_buyer_type : selectSeasonId,
                    back_buyer_refusal :that.data.back_buyer_refusal
                };
                Tools.showLoaddingDefault();
                //if(upladImgData.length == 0){
                requestMode.sendRequest('order/back','POST',formData,{sKey:session.get('sKey')}).then(function(rejectedSuccess){
                    let code = rejectedSuccess.data.code;
                    Tools.hideLoaddingToast();
                    if(code == 200){
                        Tools.showSuccessInfoToast('操作成功');
                        wx.redirectTo({
                            url:'../seeMinesOrder/seeMinesOrder?type=4'
                        })
                    }else{
                        Tools.showErrorToast(rejectedSuccess.data.message);
                    }
                }, function () {
                    Tools.showWrongNetWorkToast();
                });
                //  }
            }else{
                    Tools.showErrorToast('请输入详细信息！')
            }
        }else{
            Tools.showErrorToast('请选择退货原因！')
        }
    },
    circulateUploadImg: function (data,current) {
        let fileLength = data.length;
        let that = this;
        let allNotFailImg = that.data.allNotFailImg;//上传失败的图片添加到这个数组
        if(!current){
            current = 0;
        }
        if( current <= fileLength - 1){
            wx.uploadFile({
                url : constants.requestUrl+'order/up-back-pictures'+constants.lastText,
                filePath: data[current],
                name : 'back_images',
                formData :{order_id:that.data.orderId},
                header : { "Content-Type": "multipart/form-data" , 'sKey':session.get('sKey') },
                success : function(uploadThisImgSuccess){
                    Tools.hideLoaddingToast();
                    that.circulateUploadImg(data,current+1)
                },
                fail: function (e) {
                    allNotFailImg.push(data[current]);
                    that.setData({
                        allNotFailImg : allNotFailImg
                    })
                }
            });
        }else{
            if(that.data.allNotFailImg.length != 0 ){
                //有图片未上传
                that.circulateUploadImg(that.data.allNotFailImg);
                that.setData({  allNotFailImg : []  })
            }else{
                //图片上传完毕  提交其他数据

            }
        }
    }
});